package com.zhouyang.imtest.config;

import com.zhouyang.imtest.interceptor.MessageHandshakeInterceptor;
import com.zhouyang.imtest.websocket.MessageHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @Author:ZhouYang
 * @Description:
 * @Date: Created in 2020/7/16 14:38
 * @Modified By：
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private MessageHandler messageHandler;

    @Autowired
    private MessageHandshakeInterceptor messageHandshakeInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(this.messageHandler, "/ws/{uid}")  //设置处理器以及拦截规则
                .setAllowedOrigins("*") //设置允许跨域
                .addInterceptors(this.messageHandshakeInterceptor); //设置自定义过滤器
    }
}

